#ifndef __EPOLL_LOOP_H__
#define __EPOLL_LOOP_H__

#include "tcp_link.hpp"
#include "acceptor.hpp"
#include "lock_guard.hpp"
#include "mutex_lock.hpp"
#include <map>
#include <memory>
#include <functional>
#include <vector>
#include <mutex>

using std::function;
using std::shared_ptr;
using std::vector;

namespace rocket 
{

class epoll_loop
{
public:
    epoll_loop(acceptor *) ;
    ~epoll_loop() ;
    void loop();
    void turn_off_loop();
    void set_callback(function<void(shared_ptr<tcp_link>)>callback_new,
            function<void(shared_ptr<tcp_link>)>callback_old,
            function<void(shared_ptr<tcp_link>)>callback_close);
    void notify(function<void ()>);
    void deal_send();
private:
    void new_link(shared_ptr<tcp_link>);
    void old_link(shared_ptr<tcp_link>);
    void close_link(shared_ptr<tcp_link>);
    void add_epoll_read_event(int);
    void delete_epoll_read_event(int);
    void send_task_callback(function<void()>);    
private:
    acceptor *_acceptor;
    std::map<int, shared_ptr<tcp_link>> _tcp_link_list;
    bool _running_status=false;  
    int _epoll;
    int _eventfd;
    vector<epoll_event>_events;
    vector<function<void()>>_send_list;
    mutex_lock _lock;
    function<void(shared_ptr<tcp_link>)> _new;
    function<void(shared_ptr<tcp_link>)> _old;
    function<void(shared_ptr<tcp_link>)> _close;
};



}

#endif